home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CUDPServerThread.cp < prev    next >
Encoding:
Text File  |  1998-08-10  |  3.2 KB  |  117 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CUDPServerThread.cp            ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4.  
  5. #include "CUDPServerThread.h"
  6.  
  7. #include <PP_KeyCodes.h>
  8.  
  9.  
  10. // ---------------------------------------------------------------------------
  11. //        • CUDPServerThread
  12. // ---------------------------------------------------------------------------
  13.  
  14. CUDPServerThread::CUDPServerThread(
  15.     UInt16                            inLocalPort,
  16.     PP_PowerPlant::LUDPEndpoint        *inNetworkEndpoint,
  17.     CSimpleUDPServer*                inServerMaster,
  18.     CTerminalPane*                    inTerminalPane)
  19.         : LThread(    false,
  20.                     PP_PowerPlant::thread_DefaultStack,
  21.                     PP_PowerPlant::LThread::threadOption_Default,
  22.                     nil),
  23.           mPort(inLocalPort),
  24.           mEndpoint(inNetworkEndpoint),
  25.           mServerMaster(inServerMaster),
  26.           mTerminalPane(inTerminalPane)
  27. {
  28.     mContinue = true;
  29. }
  30.  
  31.  
  32. // ---------------------------------------------------------------------------
  33. //        • ~CUDPServerThread
  34. // ---------------------------------------------------------------------------
  35.  
  36. CUDPServerThread::~CUDPServerThread()
  37. {
  38. }
  39.  
  40. // ---------------------------------------------------------------------------
  41. //        • Complete
  42. // ---------------------------------------------------------------------------
  43.  
  44. void
  45. CUDPServerThread::Complete()
  46. {
  47.     if (mContinue) {
  48.         mContinue = false;
  49.         mEndpoint->AbortThreadOperation(this);
  50.     }
  51. }
  52.  
  53. // ---------------------------------------------------------------------------
  54. //        • Run
  55. // ---------------------------------------------------------------------------
  56.  
  57. void*
  58. CUDPServerThread::Run()
  59. {
  60.     try {
  61.  
  62.         // Initialization: Bind to the port specified
  63.         PP_PowerPlant::LInternetAddress address(0, mPort);
  64.         mEndpoint->Bind(address, 1);
  65.         mServerMaster->BindCompleted();
  66.         
  67.         // Endless loop: watch and wait for incomming UDP data.
  68.         while (mContinue) {
  69.             Try_ {
  70.                 PP_PowerPlant::LInternetAddress    remoteAddress;
  71.                 char                 dataBuffer[80];
  72.                 UInt32                ioDataSize = 80;
  73.                 
  74.                 mEndpoint->ReceiveFrom(remoteAddress, dataBuffer, ioDataSize);
  75.                 
  76.                 //Get the IP Address/Port of the sender
  77.                 Str255 remoteAddressString;
  78.                 remoteAddress.GetIPDescriptor(remoteAddressString, true);
  79.                 PP_PowerPlant::LStr255 remoteAddressDisplay = remoteAddressString;
  80.                     
  81.                 //Add CRLF
  82.                 remoteAddressDisplay += PP_PowerPlant::char_Return;
  83.                 remoteAddressDisplay += PP_PowerPlant::char_LineFeed;
  84.     
  85.                 //Display the remote address info
  86.                 mTerminalPane->DoWriteBfr((const char *)&remoteAddressDisplay[1], remoteAddressDisplay[0]);
  87.                 
  88.                 //Display the UDP data
  89.                 mTerminalPane->DoWriteBfr(dataBuffer, ioDataSize);
  90.                 
  91.                 //Turn the data back arround to the sender
  92.                 mEndpoint->SendPacketData(remoteAddress, dataBuffer, ioDataSize);
  93.             }
  94.             Catch_ (inErr) {
  95.                 //Examples of things to watch for
  96.                 if (inErr == PP_PowerPlant::Timeout_Error) {
  97.                     continue;                    // you might want to break here instead
  98.                 }
  99.                 else if (inErr == PP_PowerPlant::Abort_Error) {
  100.                     break;
  101.                 }
  102.                 else {
  103.                     break;
  104.                 }
  105.             }
  106.         }
  107.     }
  108.  
  109.     catch(...) {
  110.         // We're done with the connection… bail out.
  111.         mEndpoint->Unbind();
  112.     }
  113.  
  114.     mServerMaster->ServerThreadDied();
  115.     return nil;
  116. }
  117.